package com.xca.photogallery.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xca.photogallery.R;
import com.xca.photogallery.entity.DayAvatar;
import com.xca.photogallery.entity.DayImage;
import com.xca.photogallery.views.AvatarShowActivity;

import java.util.List;
import java.util.Random;

public class AvatarFirstAdapter extends RecyclerView.Adapter<AvatarFirstAdapter.ViewHolder>{
    private List<DayAvatar.AvatarFirstData> mList ;
    private List<DayAvatar.AvatarFirstData.AvatarSecondData> sList ;
    private Context context ;
    public AvatarFirstAdapter(List<DayAvatar.AvatarFirstData> list , Context context){
        this.mList = list ;
        this.context = context ;
    }
    public void updateData(List<DayAvatar.AvatarFirstData> list){
        mList.clear();
        mList = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_item , parent , false);
        return new AvatarFirstAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DayAvatar.AvatarFirstData data = mList.get(position);
        sList = data.getList();
        Random random = new Random();
        int s = random.nextInt(sList.size());
        Glide.with(context)
                .load(sList.get(s).getIcon())
                .into(holder.imageView);
        holder.textView.setText(data.getName() + "(" + data.getNum() + ")");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context , AvatarShowActivity.class);
                intent.putExtra("name" , data.getName());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView ;
        TextView textView ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.a_item_img);
            textView = itemView.findViewById(R.id.a_item_text);
        }
    }
}
